//
//  AppDelegate.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/4.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func versionCheck() {
        // 获取当前的版本号
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        let sandboxVersion = UserDefaults.standard.object(forKey: "CFBundleShortVersionString") as? String ?? ""
        if currentVersion.compare(sandboxVersion) == ComparisonResult.orderedDescending {
            //存储当前的版本到沙盒
            //获取到的当前版本 > 之前的版本 = 有新版本
            Defaults[.needUpdate] = true
            
        } else {
            //获取到的当前版本 <= 之前的版本 = 没有新版本
            Defaults[.needUpdate] = false
        }
    }

    
    // 初始化界面
    func initView() {
        //versionCheck()
        initAppearance()
        LAVersion.check()
        if Defaults[.setGestureLock] == true {
            let nextView = CheckGestureController()
            self.window?.rootViewController = nextView
        } else {
            let mainView = TabMainViewController()
            self.window?.rootViewController = mainView
        }
        
        self.window?.makeKeyAndVisible()
    }
    
    // 初始化样式
    func initAppearance() {
        // 修改状态颜色
        UIApplication.shared.statusBarStyle = .lightContent
        // navigation 样式
        let naviAppearance = UINavigationBar.appearance()
        // 取消颜色转换
        naviAppearance.isTranslucent = false
        // 取消黑线
        naviAppearance.setBackgroundImage(UIImage(), for: .default)
        // 设置 navigationbar 栏的颜色
        naviAppearance.barTintColor = UIColor.naviBarTint
        // 设置文本颜色 控件颜色：例如：title 和 返回按钮
        naviAppearance.tintColor = UIColor.white
        // 设置标题属性
        naviAppearance.titleTextAttributes = [NSFontAttributeName : UIFont.la_Font(ofSize: 20),NSForegroundColorAttributeName : UIColor.white]
        
        // 设置 tabBar 栏样式
        let tabBarAppearance = UITabBar.appearance()
        // 取消颜色转换
        tabBarAppearance.isTranslucent = false
        // 设置 tabBar 栏的颜色
        tabBarAppearance.barTintColor = UIColor.white
        // tabBar 的 TintColor 会改变选中时 的图标颜色
        tabBarAppearance.tintColor = UIColor.naviBarTint
        // 设置 tabBarItem 的样式属性
        let tabBarItemAppearance = UITabBarItem.appearance()
        // 设置 tabBarItem 字体大小，颜色，已经被选中后的颜色
        tabBarItemAppearance.setTitleTextAttributes([NSFontAttributeName : UIFont.la_Font(ofSize: 9),NSForegroundColorAttributeName : UIColor.tabBarText(.normal)], for: .normal)
        tabBarItemAppearance.setTitleTextAttributes([NSFontAttributeName : UIFont.la_Font(ofSize: 9),NSForegroundColorAttributeName : UIColor.tabBarText(.highlighted)], for: .selected)
    }
    
    // 初始化友盟
    func initUmeng() {
        MobClick.setLogEnabled(true)
        let conf = UMAnalyticsConfig.init()
        conf.appKey = "5942457b2ae85b73b90019ee"
        MobClick.start(withConfigure: conf)
        let version =  Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        MobClick.setAppVersion(version)
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        initView()
        initUmeng()
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

